package com.zhx.ad.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhx.ad.common.exception.AdException;
import com.zhx.ad.constants.Constants;
import com.zhx.ad.entity.AdPlan;
import com.zhx.ad.entity.AdUser;
import com.zhx.ad.enums.CommonStatus;
import com.zhx.ad.mapper.AdPlanMapper;
import com.zhx.ad.service.IAdPlanService;
import com.zhx.ad.service.IAdUserService;
import com.zhx.ad.utils.CommonUtils;
import com.zhx.ad.client.vo.AdPlanGetRequest;
import com.zhx.ad.client.vo.AdPlanRequest;
import com.zhx.ad.client.vo.AdPlanResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *describe: 
 *
 *@author zhanghaixuan
 *@date 2019/02/02
 **/
@Service
public class AdPlanServiceImpl extends ServiceImpl<AdPlanMapper, AdPlan> implements IAdPlanService {

	@Autowired
	private IAdUserService userService;

	@Override
	public AdPlan findByIdAndUserId(String id, String userId) {
		return baseMapper.findByIdAndUserId(id,userId);
	}

	@Override
	public List<AdPlan> findAllByIdInAndUserId(List<String> ids, String userId) {
		Map<String,Object> map = new HashMap<String,Object>(2);

		map.put("ids",ids);
		map.put("userId",userId);

		return baseMapper.findAllByIdInAndUserId(map);
	}

	@Override
	public AdPlan findByUserIdAndPlanName(String userId, String planName) {
		return baseMapper.findByUserIdAndPlanName(userId,planName);
	}

	@Override
	public List<AdPlan> findAllByPlanStatus(Integer status) {
		return baseMapper.findAllByPlanStatus(status);
	}



	@Override
	@Transactional
	public AdPlanResponse createAdPlan(AdPlanRequest request)
			throws AdException {

		if (!request.createValidate()) {
			throw new AdException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
		}

		// 确保关联的 User 是存在的
		AdUser adUser = userService.getById(request.getUserId());
		if (adUser == null) {
			throw new AdException(Constants.ErrorMsg.CAN_NOT_FIND_RECORD);
		}

		AdPlan oldPlan = findByUserIdAndPlanName(
				request.getUserId(), request.getPlanName()
		);
		if (oldPlan != null) {
			throw new AdException(Constants.ErrorMsg.SAME_NAME_PLAN_ERROR);
		}

		AdPlan adPlan = new AdPlan(request.getUserId(), request.getPlanName(),
				CommonUtils.parseStringDate(request.getStartDate()), CommonUtils.parseStringDate(request.getEndDate()));

		save(adPlan);

		return new AdPlanResponse(adPlan.getId(),
				adPlan.getPlanName());
	}

	@Override
	public List<AdPlan> getAdPlanByIds(AdPlanGetRequest request)
			throws AdException {

		if (!request.validate()) {
			throw new AdException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
		}

		return findAllByIdInAndUserId(
				request.getIds(), request.getUserId()
		);
	}

	@Override
	@Transactional
	public AdPlanResponse updateAdPlan(AdPlanRequest request)
			throws AdException {

		if (!request.updateValidate()) {
			throw new AdException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
		}

		AdPlan plan = findByIdAndUserId(
				request.getId(), request.getUserId()
		);
		if (plan == null) {
			throw new AdException(Constants.ErrorMsg.CAN_NOT_FIND_RECORD);
		}

		if (request.getPlanName() != null) {
			plan.setPlanName(request.getPlanName());
		}
		if (request.getStartDate() != null) {
			plan.setStartDate(
					CommonUtils.parseStringDate(request.getStartDate())
			);
		}
		if (request.getEndDate() != null) {
			plan.setEndDate(
					CommonUtils.parseStringDate(request.getEndDate())
			);
		}

		plan.setUpdateTime(new Date());
		baseMapper.insert(plan);

		return new AdPlanResponse(plan.getId(), plan.getPlanName());
	}

	@Override
	@Transactional
	public void deleteAdPlan(AdPlanRequest request) throws AdException {

		if (!request.deleteValidate()) {
			throw new AdException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
		}

		AdPlan plan = findByIdAndUserId(
				request.getId(), request.getUserId()
		);
		if (plan == null) {
			throw new AdException(Constants.ErrorMsg.CAN_NOT_FIND_RECORD);
		}

		plan.setPlanStatus(CommonStatus.INVALID.getStstus());
		plan.setUpdateTime(new Date());
		baseMapper.insert(plan);
	}
}
